package com.example.demo01.Impl.webImpl;

import com.example.demo01.Dao.webDao.ClientUserDao;
import com.example.demo01.Entity.webEntity.ClientUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Repository
public class ClientUserDaoImpl implements ClientUserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Map<String, Object>> getAllClientUsers() {
        String sqlQuery="SELECT * FROM `userinfo1`";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sqlQuery);
        return list;
    }

    @Override
    public ClientUserInfo getDetailInfoByIdNum(String idNum) {
        String sqlQuery="SELECT * FROM `userinfo1` WHERE userinfo1.idnum=?";
        return jdbcTemplate.queryForObject(sqlQuery,new Object[]{idNum}, new RowMapper<ClientUserInfo>() {
            @Nullable
            @Override
            public ClientUserInfo mapRow(ResultSet resultSet, int i) throws SQLException {
                return  new ClientUserInfo(
                        resultSet.getString(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getString(5),
                        resultSet.getString(6).charAt(0),
                        resultSet.getString(7),
                        resultSet.getString(8),
                        resultSet.getString(9),
                        resultSet.getString(10),
                        resultSet.getFloat(11),
                        resultSet.getFloat(12)
                );
            }
        });
    }
}
